<?php
include_once './include/common.inc.php';
include_once './include/plugins.class.php';
include_once 'hook.class.php';
$plugins = new plugins;
$action = isset($action) ? $action : 'manage';
$forward = get_forward('/admin/group.php');
$hook_obj = new hook();
$hook_list = $hook_obj->hooks;
$site_list = site_list();
$plug_list = $plugins->get_plugins();
switch($action)
{
    case 'add' : //添加内容
    	if(isset($_POST['hook']))
    	{
    		if(empty($_POST['plug']))
    		{
    			$response->text_alert('error','plug','请选择插件');	
    		}
    		if(empty($_POST['func']))
    		{
    			$response->text_alert('error','func','请选择方法');	
    		}
    		if(empty($_POST['hook']))
    		{
    			$response->text_alert('error','hook','请选择钩子');	
    		}
    		$res = $plugins->get_plugin_funcs($_POST['plug']);
    		if(!isset($res[$_POST['func']]))
    		{
    			$response->text_alert('error','func','您选择的方法不存在');	
    		}
    		if(!isset($plug_list[$_POST['plug']]))
    		{
    			$response->text_alert('error','plug','您选择的插件不存在');	
    		}
    		if(!isset($hook_list[$_POST['hook']]))
    		{
    			$response->text_alert('error','hook','您选择的钩子不存在');	
    		}
    		$response->real_execute();
    		$ck_res = $plugins->check_func_hook_add($_POST['plug'],$_POST['func'],$_POST['siteid']);
    		if($ck_res === true)
    		{
    			$response->alert('此关联已添加，不能重复添加');
    			$response->execute();
    		}
    		
    		$params = array(
				'plugins'		=>	$_POST['plug'],
				'func'			=>	$_POST['func'],
				'funcinfo'		=>	$res[$_POST['func']],
				'hook'			=>	$_POST['hook'],
				'hookinfo'		=>	$hook_list[$_POST['hook']],
				'pluginsinfo'	=>	$plug_list[$_POST['plug']][2],
				'status'		=>	STATUS_NORMAL,
				'siteid'		=>	$_POST['siteid'],
			);
    		$res = $plugins->add_func_hook($params);
    		if($res === false)
    		{
    			$response->alert('添加失败，请稍后再试');
    		}
    		else 
    		{
    			$response->alert('添加成功');
    			$response->redirect($_POST['forward']);
    		}
    		$response->execute();
    	}
    	else 
    	{
    		include template('admin', 'plugins_add');
    	}
    	break;
    case 'getfuncs':
    	$plug = $_GET['plug'];
    	$funcs = $plugins->get_plugin_funcs($plug);
    	$html = '';
    	if(is_array($funcs))
    	{
    		foreach ($funcs as $k=>$f)
    		{
    			$html .= '<option value=\''.$k.'\'>'.$f.'</option>';
    		}
    	}
    	$response->script('$("#func").empty();');
    	$response->script('$("#func").append(" '.$html.'");');
    	$response->execute();
    	break;
    case 'status':
    	$res = $plugins->change_status($id);
    	if($res === false)
    	{
    		$response->alert('修改失败');
    	}
    	else 
    	{
    		$response->alert('修改成功');
    		$response->script('self.location=self.location;');
    	}
    	$response->execute();
    	break;
    case 'delete':
    	$res = $plugins->delete($id);
    	if($res === false)
    	{
    		$response->alert('删除失败');
    	}
    	else 
    	{
    		$response->alert('删除成功');
    		$response->script('self.location=self.location;');
    	}
    	$response->execute();
    	break;
    case 'search';
    	$url = 'plugins.php?page=1';
    	if( !empty( $siteid ) )
        {
        	$url .= isset( $siteid ) && !empty( $siteid ) ? '&siteid='.urlencode( $siteid ) : '';
        }
        if( !empty( $plug ) )
        {
            $url .= isset( $plug ) && !empty( $plug ) ? '&plug='.urlencode( $plug ) : '';
        }
        if( !empty( $hook ) )
        {
            $url .= isset( $hook ) && !empty( $hook ) ? '&hook='.urlencode( $hook ) : '';
        }
		$response->redirect( $url );
		$response->execute();
    	break;
    default:
    	$condition['siteid'] = isset($siteid) ? $siteid : '';
        $condition['plugins'] = isset($plug) ? $plug : '';
        $condition['hook'] = isset($hook) ? $hook : '';
        $page = isset($page) ? intval($page) : 1;
        $data = $plugins->listinfo($condition, $page, 50);
        $pages = $plugins->pages;
        $forward = make_forward();
        include template('admin', 'plugins_manages');
}
?>